﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using DataAccess.DB;
using WebMatrix.WebData;

namespace DuocThaiBinh.Controllers
{
    public class CuaHangController : Controller
    {
        private db_YTeTBEntities db = new db_YTeTBEntities();

        [HttpPost]
        public JsonResult ChangeCuaHang(int cuaHangID)
        {
            UserInCuaHangController userInCuaHangController = new UserInCuaHangController();
            if (userInCuaHangController.CheckUserInCuaHang(WebSecurity.GetUserId(User.Identity.Name), cuaHangID))
            {
                Session["CuaHangID"] = cuaHangID;
                return Json("done", JsonRequestBehavior.AllowGet);
            }

            return Json("failed", JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /CuaHang/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Administrator")]
        public JsonResult IndexGet()
        {
            var chs = db.CuaHangs.OrderBy(p => p.TenCuaHang).Where(p => p.Status).ToList();

            return Json(new
            {
                data = chs.Select(x => new { TenCuaHang = x.TenCuaHang, ID = x.ID })
            }, JsonRequestBehavior.AllowGet);
        }
        
        //
        // GET: /CuaHang/Create

        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /CuaHang/Create

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(CuaHang cuahang)
        {
            if (ModelState.IsValid)
            {
                cuahang.Status = true;
                db.CuaHangs.Add(cuahang);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(cuahang);
        }

        //
        // GET: /CuaHang/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id = 0)
        {
            CuaHang cuahang = db.CuaHangs.Find(id);
            if (cuahang == null)
            {
                return HttpNotFound();
            }
            return View(cuahang);
        }

        //
        // POST: /CuaHang/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(CuaHang cuahang)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cuahang).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(cuahang);
        }

        //
        // POST: /CuaHang/Delete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult Delete(int id)
        {
            CuaHang cuahang = db.CuaHangs.Find(id);
            cuahang.Status = false;
            db.Entry(cuahang).State = EntityState.Modified;
            db.SaveChanges();
            return Json(new { msg = "success" });
        }
        
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}